package com.xjd.structs2.action;

import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;

public class UserAction implements SessionAware, ApplicationAware {
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String logout() {
		Integer count = (Integer) application.get("count");
		if (count != null && count > 0) {
			count--;
			application.put("count", count);
		}
		((SessionMap<String, Object>) session).invalidate();
		return "logout-success";
	}

	public String execute() {
		session.put("username", username);
		Integer count = (Integer) application.get("count");
		if (count == null) {
			count = 0;
		}
		count++;
		application.put("count", count);
		return "login-success";
	}

	private Map<String, Object> session;

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	private Map<String, Object> application;

	@Override
	public void setApplication(Map<String, Object> arg0) {
		this.application = arg0;
	}
}
